# Maintainer: Antoni Aloy <aaloytorrens@gmail.com>
pkgname=sgt-puzzles
pkgver=0_git20230310
_gitrev=8c5077e
pkgrel=1
pkgdesc="Simon Tatham's Portable Puzzle Collection"
url="https://www.chiark.greenend.org.uk/~sgtatham/puzzles/"
arch="all"
license="GPL-2.0-or-later"
makedepends="
	cmake
	gtk+3.0-dev
	imagemagick
	perl-dev
	samurai
	"
options="!check" # No testsuite

# We should append "sgt-" prefix to all the programs and icons.
# That way, we avoid conflicts with other programs
source="
	$pkgname-$pkgver.tar.gz::https://git.tartarus.org/?p=simon/puzzles.git;a=snapshot;h=$_gitrev;sf=tgz
	0001-change-desktop-variables-according-to-prefix.patch
	"
_icondir="usr/share/pixmaps"
_desktopdir="usr/share/applications"
builddir="$srcdir/puzzles-$_gitrev"

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	CXXFLAGS="$CXXFLAGS -flto=auto" \
	CFLAGS="$CFLAGS -flto=auto" \
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		$CMAKE_CROSSOPTS .
	cmake --build build
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	# Append "sgt-" prefix to icon files
	# Move icons to hicolor directory, like upstream distributions do
	# And remove "-48d24" suffix
	mkdir -p "$pkgdir"/usr/share/icons/hicolor/48x48/apps
	for f in "$pkgdir"/"$_icondir"/*.png; do
		mv -v -- "$f" \
			"$pkgdir"/usr/share/icons/hicolor/48x48/apps/sgt-"$(basename -- $f | sed 's/-48d24//g')"
	done

	# Append "sgt-" prefix to desktop files
	for f in "$pkgdir"/"$_desktopdir"/*.desktop; do
		mv -v -- "$f" \
			"$pkgdir"/usr/share/applications/sgt-"$(basename -- $f)"
	done

	# Remove redundant directory
	rm -r "$pkgdir"/usr/share/pixmaps
}

sha512sums="
66a4b3f1ed5778c921b6378fc6ffa43aa111f69d165ac0ee18c2442b04913676839e64e79e34edf1bf2109cc81bb580534bcd499263afce83645df5901a13060  sgt-puzzles-0_git20230310.tar.gz
ebe33479a779abf9cd0ea12232ab2319fa279ba467ee635460962eb714c87f2a87e82b160fc43665b0e9e39b5ba72d6bcc2c0cb35abe0adde6de40c18775bee6  0001-change-desktop-variables-according-to-prefix.patch
"
